package com.jc.subject.domain.service.impl;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.MapUtils;
import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.domain.convert.SubjectCategoryConverter;
import com.jc.subject.domain.entity.SubjectCategoryDO;
import com.jc.subject.domain.entity.SubjectLabelDO;
import com.jc.subject.domain.service.SubjectCategoryDomainService;
import com.jc.subject.domain.util.CacheUtil;
import com.jc.subject.infra.basic.entity.SubjectCategory;
import com.jc.subject.infra.basic.entity.SubjectLabel;
import com.jc.subject.infra.basic.entity.SubjectMapping;
import com.jc.subject.infra.basic.service.SubjectCategoryService;
import com.jc.subject.infra.basic.service.SubjectLabelService;
import com.jc.subject.infra.basic.service.SubjectMappingService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author gongjiawei
 * @Date 2024/6/5 21:22
 * @注释
 */


@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

@Resource
private SubjectCategoryService subjectCategoryService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private CacheUtil cacheUtil;
    @Override
    public void add(SubjectCategoryDO subjectCategoryDO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryDO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    @Override
    public List<SubjectCategoryDO> queryCategory(SubjectCategoryDO subjectCategoryDO) {

        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryDO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategories = subjectCategoryService.queryCategory(subjectCategory);
        if (log.isInfoEnabled()){
            log.info("queryCategory,message:{}", JSON.toJSONString(subjectCategories));
        }
        List<SubjectCategoryDO> subjectCategoryDOS = SubjectCategoryConverter.INSTANCE.convertCategoryListToDoList(subjectCategories);

        subjectCategoryDOS.forEach(
                bo->{
                   Integer subjectCount= subjectCategoryService.querySubjectCount(bo.getId());
                   bo.setCount(subjectCount);
                }
        );
        return subjectCategoryDOS;
    }

    @Override
    public Boolean update(SubjectCategoryDO subjectCategoryDO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryDO);
        int update = subjectCategoryService.update(subjectCategory);
        return update>0;
    }

    @Override
    public Boolean delete(SubjectCategoryDO subjectCategoryDO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryDO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int update = subjectCategoryService.update(subjectCategory);
        return update>0;
    }
    @SneakyThrows
    @Override
    public List<SubjectCategoryDO> queryCategoryAndLabel(SubjectCategoryDO subjectCategoryDO) {
        SubjectCategory subjectCategory=new SubjectCategory();
        subjectCategory.setParentId(subjectCategoryDO.getParentId());
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.queryCategoryAndLabel.subjectCategoryList:{}",
                    JSON.toJSONString(subjectCategoryList));
        }
        List<SubjectCategoryDO> categoryBOList = SubjectCategoryConverter.INSTANCE.convertCategoryListToDoList(subjectCategoryList);



        List<FutureTask<Map<Long,List<SubjectCategoryDO>>>>futureTaskList=new LinkedList<>();
        categoryBOList.forEach(category-> {
                    SubjectMapping subjectmapping = new SubjectMapping();
                    subjectmapping.setCategoryId(category.getId());
            List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectmapping);
            if (CollectionUtils.isEmpty(subjectMappingList))return;

            List<Long> collect = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
            List<SubjectLabel> batchquery = subjectLabelService.batchquery(collect);
            List<SubjectLabelDO> labelDOS=new LinkedList<>();
            batchquery.forEach(
                    label->{
                        SubjectLabelDO subjectLabelDO=new SubjectLabelDO();
                        subjectLabelDO.setId(label.getId());
                        subjectLabelDO.setLabelName(label.getLabelName());
                        subjectLabelDO.setCategoryId(label.getCategoryId());
                        subjectLabelDO.setSortNum(label.getSortNum());
                        labelDOS.add(subjectLabelDO);
                    });
            category.setLabelDOList(labelDOS);
                }
        );

        return categoryBOList;
    }

}
